// API基础路径配置
const API_BASE_URL = 'http://localhost:8080/api';

// API路径配置
const API_PATHS = {
  LOGIN: '/user/login',
  EVENT: {
    LIST: '/event/listByUser',
    ADD: '/event/add',
    UPDATE: '/event/update',
    DELETE: '/event/delete',
    SEARCH: '/event/search'
  },
  CATEGORY: {
    LIST: '/category/list',
    ADD: '/category/add',
    DELETE: '/category/delete'
  }
};

// 用户相关API
const userApi = {
  logout: async () => {
    try {
      console.log('开始发送退出请求'); // 添加调试日志
      const response = await fetch('http://localhost:8080/user/logout', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },

      });
      console.log('收到退出响应:', response); // 添加调试日志
      if (!response.ok) {
        throw new Error(`HTTP error! status: ${response.status}`);
      }
      return await response.json();
    } catch (error) {
      console.error('退出请求出错:', error);
      return { code: -1, message: error.message };
    }
  },

  getUserByUsername: async (username) => {
    const response = await fetch(`http://localhost:8080/user/getUserByUsername/${username}`);
    return response.json();
  }
};

// 分类相关API
const categoryApi = {
  getAll: async (username) => {
    const response = await fetch(`http://localhost:8080/eventcategorie/getAll/${username}`);
    return response.json();
  },

  add: async (data) => {
    console.log('添加分类请求数据:', data); // 调试日志
    const response = await fetch('http://localhost:8080/eventcategorie/add', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify(data)
    });
    const result = await response.json();
    console.log('添加分类响应结果:', result); // 调试日志
    return result;
  },

  get: async (categoryId) => {
    const response = await fetch(`http://localhost:8080/eventcategorie/get/${categoryId}`);
    return response.json();
  },

  update: async (data) => {
    console.log('更新分类请求数据:', data);
    const response = await fetch('http://localhost:8080/eventcategorie/update', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify(data)
    });
    const result = await response.json();
    console.log('更新分类响应结果:', result);
    return result;
  },

  delete: async (categoryId) => {
    const response = await fetch('http://localhost:8080/eventcategorie/delete', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ categoryId })
    });
    return response.json();
  }
};

// 事件相关API
const eventApi = {
  add: async (data) => {
    const response = await fetch('http://localhost:8080/event/add', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify(data)
    });
    return response.json();
  },

  getList: async (userId) => {
    try {
      const response = await fetch(`http://localhost:8080/event/list/${userId}`);
      if (!response.ok) {
        throw new Error('获取事件列表失败');
      }
      return await response.json();
    } catch (error) {
      console.error('获取事件列表错误:', error);
      return { code: -1, message: error.message };
    }
  },

  getListByUser: async (userId) => {
    try {
      console.log('开始获取用户事件列表, userId:', userId);
      const url = `http://localhost:8080/event/listByUser/${userId}`;
      const response = await fetch(url);

      if (!response.ok) {
        throw new Error(`HTTP error! status: ${response.status}`);
      }

      const data = await response.json();
      console.log('获取事件列表响应:', data);
      return data;
    } catch (error) {
      console.error('获取事件列表错误:', error);
      return {
        code: -1,
        message: '获取事件列表失败: ' + (error.message || '未知错误')
      };
    }
  },

  get: async (eventId) => {
    const url = `http://localhost:8080/event/get/${eventId}`;
    const response = await fetch(url);
    return response.json();
  },

  update: async (data) => {
    const response = await fetch('http://localhost:8080/event/update', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify(data)
    });
    return response.json();
  },

  delete: async (eventId) => {
    const response = await fetch(`http://localhost:8080/event/delete/${eventId}`, {
      method: 'GET'
    });
    return response.json();
  },

  updateStatus: async (eventId, status) => {
    const response = await fetch(`http://localhost:8080/event/updateStatus?eventId=${eventId}&status=${status}`, {
      method: 'GET'
    });
    return response.json();
  },

  updateStatusToVoid: async (eventId) => {
    const response = await fetch(`http://localhost:8080/event/updateStatus_zf/${eventId}`, {
      method: 'GET'
    });
    return response.json();
  },

  search: async (searchParams) => {
    try {
      // 将参数拼接到URL中
      const queryParams = new URLSearchParams({
        userId: searchParams.userId,
        title: searchParams.title || '',
        categoryId: searchParams.categoryId || '',
        level: searchParams.level || '',
        status: searchParams.status || ''
      }).toString();

      const url = `http://localhost:8080/event/search?${queryParams}`;
      console.log('搜索请求URL:', url); // 调试日志

      const response = await fetch(url, {
        method: 'GET',
        headers: {
          'Accept': 'application/json'
        }
      });

      if (!response.ok) {
        throw new Error(`HTTP error! status: ${response.status}`);
      }

      const data = await response.json();
      console.log('搜索响应数据:', data); // 调试日志
      return data;
    } catch (error) {
      console.error('搜索请求失败:', error);
      return {
        code: -1,
        message: '搜索失败: ' + error.message
      };
    }
  }
};
